var token = getCookie("token");
$.post('htz/user/token', { token, token }, function (res) {
    var userId = res.userId;
layui.use('table', function () {
    var table = layui.table;

    table.render({
        elem: '#test'
        , id: 'testReload'
        , where: {staffId:userId}
        , url: 'jay/PickOrder/getPickOrderList' //数据接口
        , parseData: function (res) {
            return {
                "code": 0, //解析接口状态
                "msg": "", //解析提示文本
                "count": res.obj.total, //解析数据长度
                "data": res.obj.list //解析数据列表
            };
        }
        , toolbar: '#bar'
        , defaultToolBar: ['default', 'print', 'export']
        , page: true //开启分页
        , limit: 10
        , cols: [[ //表头
            { field: 'id', title: '序号', align: 'center', width: 100, fixed: 'left', sort: true, totalRowText: '原始合计' }
            , { field: 'goodsName', title: '商品名称', align: 'center', width: 200 }
            , { field: 'goodsAllocationCode', title: '货位位置', align: 'center', width: 200 }
            , { field: 'quantityOfGoods', title: '取货数', align: 'center', width: 120, edit: 'text', totalRow: true }
            , { field: 'userName', title: '负责人', align: 'center', width: 140 }
            , {
                field: 'status', title: '状态', align: 'center', templet: function (obj) {
                    if (obj.status == 2) {//出库状态表示5则为完成
                        return "已完成";
                    } else {
                        return "未完成";
                    }
                }
            }
            , { fixed: 'right', toolbar: '#bar1', title: '操作', align: 'center' }
        ]]
    });
    var $ = layui.$, active = {
        reload: function () {
            table.reload('testReload', {
                page: {
                    curr: 1//默认从第一页开始加载
                },
                where: {
                    staffId: $("#staffId").val(),
                    shelfId: $("#shelfId").val(),
                    status: $("#status").val()
                }
            });
        }
    };
    $('#find').on('click', function () {
        var type = 'reload';
        active[type] ? active[type].call(this) : '';
    });
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event = 'finish') {
            if (data.status == 1) {
                layer.confirm("确认要完成该项任务吗", { btn: ["确认", "取消"] },
                    function () {
                        var pickorderDetail = {
                            "id": data.id,
                            "outBoundId": data.outBoundId,
                            "goodsId": data.goodsId,
                            "quantityOfGoods": data.quantityOfGoods,
                            "shelfId": data.shelfId
                        };
                        $.ajax({
                            url: 'jay/PickOrder/finishTask',
                            data: JSON.stringify(pickorderDetail),
                            contentType: "application/json;charset=UTF-8;",
                            type: "post",
                            traditional: true,
                            dataType: "json",
                            success: function (data) {
                                if (data.obj != null) {
                                    layer.msg("拣货任务完成", { icon: 1 });
                                    setTimeout(function () {
                                        location.href = 'addOrder.html';
                                    }, 500);
                                    location.href = "pickOrder.html"
                                } else {
                                    layer.msg("操作失败,请稍后再试", { icon: 2 });
                                }
                            }
                        })
                    }, function () {
                        layer.msg("您取消了操作,", { icon: 2 })
                    })
            } else if (data.status == 2) {
                layer.msg("该明细项已完成，请勿重复点击", { icon: 2 });
            }
        }
    })
});
});